package com.studycloud.tjshareuniversity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tojoy.shop.TJShopEngine;
import com.tojoy.shop.config.TJShopEngineBuilder;
import com.tojoy.shop.manage.TJShopEngineCallback;
import com.tojoy.shop.util.LogUtil;
import com.tojoy.shop.util.Utils;


/**
 * @Author: wentaozhang
 * @CreateDate: 2022/4/26 4:49 下午
 * @Email：zhangwentao01@tojoy.com
 */
public class MainActivity extends AppCompatActivity {

    EditText APPID;
    EditText AppKey;
    EditText AppSckt;
    EditText ButtonColor;
    EditText FontColor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }


    private void initUI() {
        findViewById(R.id.init_view).setOnClickListener(view -> doInit());

        findViewById(R.id.login_view).setOnClickListener(view -> doLogin());

        findViewById(R.id.shopCar_view).setOnClickListener(view -> TJShopEngine.skipShopCar(MainActivity.this));
        findViewById(R.id.order_view).setOnClickListener(view -> TJShopEngine.skipOrderCenter(MainActivity.this));
        findViewById(R.id.address_view).setOnClickListener(view -> TJShopEngine.skipAddressList(MainActivity.this));
        findViewById(R.id.health_view).setOnClickListener(view -> TJShopEngine.skipHealthBean(MainActivity.this));

        findViewById(R.id.re_init_view).setOnClickListener(view -> {
            findViewById(R.id.llv_login).setVisibility(View.GONE);
            findViewById(R.id.llv_init).setVisibility(View.VISIBLE);
        });

        findViewById(R.id.logout_view).setOnClickListener(view -> {

            TJShopEngine.logout(MainActivity.this);

            findViewById(R.id.llv_function).setVisibility(View.GONE);
            findViewById(R.id.llv_login).setVisibility(View.VISIBLE);
            findViewById(R.id.llv_init).setVisibility(View.GONE);
        });

        findViewById(R.id.page_view).setOnClickListener(view -> skipPage());

        APPID = (EditText)findViewById(R.id.edit_appId);
        AppKey = (EditText)findViewById(R.id.edit_appKey);
        AppSckt = (EditText)findViewById(R.id.edit_appSecret);
        ButtonColor = (EditText)findViewById(R.id.edit_bgColor);
        FontColor = (EditText)findViewById(R.id.edit_fontColor);

        if (!TextUtils.isEmpty(Utils.getCache(this,"APPID"))) {
            APPID.setText(Utils.getCache(this,"APPID"));
        }

        if (!TextUtils.isEmpty(Utils.getCache(this,"APPKEY"))) {
            AppKey.setText(Utils.getCache(this,"APPKEY"));
        }

        if (!TextUtils.isEmpty(Utils.getCache(this,"APPSecret"))) {
            AppSckt.setText(Utils.getCache(this,"APPSecret"));
        }

        if (!TextUtils.isEmpty(Utils.getCache(this,"Mobile"))) {
            ((EditText)findViewById(R.id.edit_mobile)).setText(Utils.getCache(this,"Mobile"));
        }

    }

    private void doInit() {
        if(APPID.getText().toString().trim().length() == 0) {
            Toast.makeText(MainActivity.this, "请输入APPID", Toast.LENGTH_SHORT).show();
            return;
        }

        if(AppKey.getText().toString().trim().length() == 0) {
            Toast.makeText(MainActivity.this, "请输入APPKey", Toast.LENGTH_SHORT).show();
            return;
        }

        if(AppSckt.getText().toString().trim().length() == 0) {
            Toast.makeText(MainActivity.this, "请输入APPSecret", Toast.LENGTH_SHORT).show();
            return;
        }

        Utils.saveCache(this,"APPID",APPID.getText().toString().trim());
        Utils.saveCache(this,"APPKEY",AppKey.getText().toString().trim());
        Utils.saveCache(this,"APPSecret",AppSckt.getText().toString().trim());

        TJShopEngineBuilder builder = new TJShopEngineBuilder();
        builder.setAppId(APPID.getText().toString().trim());
        builder.setAppKey(AppKey.getText().toString().trim());
        builder.setAppSecret(AppSckt.getText().toString().trim());

        if(ButtonColor.getText().toString().trim().length() == 0) {
            builder.setThemeBgColor(ButtonColor.getText().toString().trim());
        }else{
            builder.setThemeBgColor("#55ceac");
        }

        if(FontColor.getText().toString().trim().length() == 0) {
            builder.setThemeFontColor(FontColor.getText().toString().trim());
        }else{
            builder.setThemeFontColor("#ffffff");
        }

        builder.setOpenDebug("true");
        builder.setWxAppId("wxd8d08b194b093706");
        TJShopEngine.initSDK(this, builder, (res, msg) -> {
            if(res) {
                Toast.makeText(MainActivity.this, "初始化成功", Toast.LENGTH_SHORT).show();
                findViewById(R.id.llv_login).setVisibility(View.VISIBLE);
                findViewById(R.id.llv_init).setVisibility(View.GONE);
            }else{
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void doLogin() {
        if(((EditText)findViewById(R.id.edit_mobile)).getText().toString().trim().length() == 0) {
            Toast.makeText(MainActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        TJShopEngine.login(((EditText) findViewById(R.id.edit_mobile)).getText().toString().trim(), new TJShopEngineCallback() {
            @Override
            public void result(boolean res, String msg) {
                if(res) {
                    Utils.saveCache(MainActivity.this,"Mobile",((EditText)findViewById(R.id.edit_mobile)).getText().toString().trim());
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    findViewById(R.id.llv_function).setVisibility(View.VISIBLE);
                    findViewById(R.id.llv_login).setVisibility(View.GONE);
                    findViewById(R.id.llv_init).setVisibility(View.GONE);
                }else{
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    private void skipPage() {
        if(((EditText)findViewById(R.id.edit_pageId)).getText().toString().trim().length() == 0) {
            Toast.makeText(MainActivity.this, "请输入业务ID", Toast.LENGTH_SHORT).show();
            return;
        }

        TJShopEngine.skipScene(MainActivity.this,((EditText)findViewById(R.id.edit_pageId)).getText().toString().trim());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            System.out.println("按下了back键 onKeyDown()");
            android.os.Process.killProcess(android.os.Process.myPid());
            // 正常退出程序，也就是结束当前正在运行的 java 虚拟机
            System.exit(0);
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }


    @Override
    protected void onDestroy() {
        System.out.println("----------onDestroy");
        TJShopEngine.release(MainActivity.this);
        super.onDestroy();
    }
}
